package donot996.icu.redis.controller;

import donot996.icu.redis.mapper.RangeMapper;
import donot996.icu.redis.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/add")
public class AddNumController {
    @Autowired
    RedisService redisService;
    @Autowired
    RangeMapper rangeMapper;

    private String AddNumKey = "add:num";

    @GetMapping("/all")
    public Map<String,Object> all(){
        Map<String, Object> cacheMap = redisService.getCacheMap(AddNumKey);

        // 初始化
        if (cacheMap.keySet().size() == 0){
            Map<String,Object> num = new HashMap<String,Object>(){
                {
                    this.put("aa",0);
                    this.put("bb",0);
                    this.put("cc",0);
                    this.put("dd",0);
                }
            };
            redisService.setCacheMap("add:num",num);
        }
        Map<String, Object> Map = redisService.getCacheMap("add:num");
        return Map;
    }

    @GetMapping("/num")
    public int addNum(String name){
        Map<String, Object> cacheMap = redisService.getCacheMap(AddNumKey);
        // 初始化
        if (cacheMap.keySet().size() == 0){
            Map<String,Object> num = new HashMap<String,Object>(){
                {
                    this.put("aa",0);
                    this.put("bb",0);
                    this.put("cc",0);
                    this.put("dd",0);
                }
            };
            redisService.setCacheMap("add:num",num);
        }
        // 加一
        redisService.incrementCacheMapValue("add:num",name,1);
        // 查出来然后返回
        return 1;
    }
}
